<?php


namespace app\admin\controller\inquiry;


use app\common\controller\Backend;
use app\Request;
use think\facade\Db;
class Inquiry extends Backend
{
    protected $noNeedLogin = ['index','detail','del'];
    protected $model = null;
//    protected $noNeedRight = ['check', 'rulelist'];

    public function initialize()
    {
        parent::initialize();
        $this->model =  Db::name('inquiry');
    }

    public function index(Request $request)
    {
        if ($this->request->isAjax()) {
            if ($request->request('keyField')) {
                return $this->selectpage();
            }
            [$where, $sort, $order, $offset, $limit] = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select()->toArray();


            $result = ['total' => $total, 'rows' => $list];
            return json($result);
        }
        $statusList = [
            '1' => '未查看',
            '2' => '已查看',
        ];
        $this -> assign('statusList',$statusList);
        return $this->fetch();
    }

    /**
     * 详情.
     */
    public function detail($ids = ''){
        //查询当前病人信息
        $binfo = $this -> model -> where('id',$ids) -> find();
        //查询最后插入的一条数据
        $info = Db::name('inquiry_log') -> where('inquiryid',$ids) -> order('createtime desc') -> limit(0,1) -> find() ;
        if($binfo['sex'] == 1){
            $binfo['sex'] = '男';
        }else if($binfo['sex'] == 2){
            $binfo['sex'] = '女';
        }else{
            $binfo['sex'] = '';
        }
        $info['images'] = explode(',',$info['inimages']);
        $this -> assign('binfo',$binfo);
        $this -> assign('info',$info);
        $this -> model -> where('id',$ids) -> update(['status'=>2]);
        return $this->fetch();
    }

    /**
     * 删除.
     */
    public function del($ids = ''){
        if ($ids) {
            Db::name('inquiry') -> where('id',$ids) -> delete();
            Db::name('inquiry_log')  -> where('inquiryid',$ids) -> delete();
            $this->success();
        }
        $this->error();
    }

}